﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Directory.Web
{
	public partial class Admin_DBManager : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			ltMsg_Bak.Text = "";
			pnlQueryResult.Controls.Clear();
			this.btnRestore.Attributes.Add("onclick", "return(confirm('恢复备份操作将丢失最后一次备份后的所有数据，是否继续？'))");
			this.btnExecuteQuery.Attributes.Add("onclick","return(confirm('执行更新、删除前请先做好备份，您是否要继续执行查询呢？'))");
			this.btnUploadBak.Attributes.Add("onclick", "return(confirmUpload('"+ddlBackUpName.ClientID+"'))");
		}

		// 备份数据库
		protected void btnBackUp_Click(object sender, EventArgs e)
		{
			try
			{
				DBQueryDataProxy.BackUpDB(ddlBackUpName.SelectedValue);
				ltMsg_Bak.Text = "备份成功。";
			}
			catch (Exception ex)
			{
				ltMsg_Bak.Text = "备份失败：" + ex.Message;
			}
		}

		// 恢复备份
		protected void btnRestore_Click(object sender, EventArgs e)
		{
			try
			{
				DBQueryDataProxy.RestoreDB(ddlBackUpName.SelectedValue);
				ltMsg_Bak.Text = "恢复成功。";
			}
			catch (ArgumentException)
			{
				ltMsg_Bak.Text = "备份名称有误。";
			}
			catch (Exception ex)
			{
				ltMsg_Bak.Text = "恢复失败：" + ex.Message;
			}
		}

		// 下载备份
		protected void btnDownloadBak_Click(object sender, EventArgs e)
		{
			Response.Clear();
			Response.ContentType = "text/bak";
			Response.AddHeader("Content-Disposition", "attachment; filename=\"" + ddlBackUpName.SelectedValue + "\"");
			string filePath = ConfigurationManager.AppSettings["DBbakDiskPath"] + ddlBackUpName.SelectedValue;
			Server.Transfer(filePath,false);
		}

		// 上传备份
		protected void btnUploadBak_Click(object sender, EventArgs e)
		{
			if (!FileUpload1.HasFile)
			{
				ltMsg_Bak.Text = "请选择要上传的备份文件！";
				return;
			}
			try
			{
				FileUpload1.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["DBbakDiskPath"] + ddlBackUpName.SelectedValue));
				ltMsg_Bak.Text = "文件上传成功，已覆盖原备份：" + ddlBackUpName.SelectedItem.Text;
				return;
			}
			catch (Exception ex)
			{
				ltMsg_Bak.Text = "服务器错误：" + ex.Message;
				return;
			}
		}

		// 执行查询
		protected void btnExecuteQuery_Click(object sender, EventArgs e)
		{
			switch (rbtnlQueryType.SelectedValue)
			{
				case "ExecuteNonQuery":
					ExecuteNonQuery();
					break;
				case "ExecuteScalar":
					ExecuteScalar();
					break;
				case "ExecuteReader":
					ExecuteReader();
					break;
				default:
					Literal lt = new Literal();
					lt.Text = "Unknown Execute Type:" + rbtnlQueryType.SelectedValue;
					pnlQueryResult.Controls.Add(lt);
					break;
			}
		}

		private void ExecuteNonQuery()
		{
			Literal lt = new Literal();
			try
			{
				lt.Text = "受影响行数：" + DBQueryDataProxy.ExecuteNonQuery(tbQuery.Text).ToString();
			}
			catch (Exception ex)
			{
				lt.Text = "执行失败：<br />" + ex.Message;
			}
			pnlQueryResult.Controls.Add(lt);
		}

		private void ExecuteScalar()
		{
			Literal lt = new Literal();
			try
			{
				lt.Text = "结果集第一行第一列：" + DBQueryDataProxy.ExecuteScalar(tbQuery.Text).ToString();
			}
			catch (Exception ex)
			{
				lt.Text = "执行失败：<br />" + ex.Message;
			}
			pnlQueryResult.Controls.Add(lt);
		}

		private void ExecuteReader()
		{
			try
			{
				DataSet ds = DBQueryDataProxy.ExecuteReader(tbQuery.Text);
				GridView gv = new GridView();
				gv.DataSource = ds;
				gv.DataBind();
				pnlQueryResult.Controls.Add(gv);
			}
			catch (Exception ex)
			{
				Literal lt = new Literal();
				lt.Text = "执行失败：<br />" + ex.Message;
				pnlQueryResult.Controls.Add(lt);
			}
		}
	}
}
